/*
 * MiscTab.java
 *
 * Created on 27. Februar 2008, 10:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.blitzcoder.collide.config;

import de.blitzcoder.collide.Config;
import de.blitzcoder.collide.util.OS;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

/**
 *
 * @author Blitzcoder
 */
public class MiscTab extends ConfigTab {
    
    private JComboBox   lafComboBox;
    private JTextField  bmkPathTextField;
    private JButton     bmkPathButton;
    
    private JPanel compilerPanel;
    private JPanel guiPanel;
    
    private static final String[] LAF = {
        "javax.swing.plaf.metal.MetalLookAndFeel",
        "com.sun.java.swing.plaf.motif.MotifLookAndFeel",
        "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel",
        getSystemLookAndFeelClassName()
    };
    private static final String[] LAFNAME = {
        "Metal (Standard)",
        "Motif",
        "Nimbus",
        "Systemthema ("+getSystemThemeName()+")"
    };

    public String getTitle() {
        return "Sonstiges";
    }

    private static String getSystemLookAndFeelClassName() {
        if (OS.isMacOSX())
            return "ch.randelshofer.quaqua.QuaquaLookAndFeel";
        return UIManager.getSystemLookAndFeelClassName();
    }

    private static String getSystemThemeName() {
        if (OS.isWindows())
            return "Windows Basic/Luna/Aero";
        if (OS.isLinux())
            return "Gtk+";
        if (OS.isMacOSX())
            return "Aqua";
        return getSystemLookAndFeelClassName();
    }

    public void createInterface() {
        compilerPanel = new JPanel();
        compilerPanel.setBorder(BorderFactory.createTitledBorder("Compiler"));
        compilerPanel.setLayout(new GridBagLayout());
        
        guiPanel = new JPanel();
        guiPanel.setBorder(BorderFactory.createTitledBorder("Oberfläche"));
        guiPanel.setLayout(new GridBagLayout());
        
        // Set up Layout
        setLayout( new GridBagLayout() );
        GridBagConstraints c = new GridBagConstraints();
        
        
        // Create Gadgets
        lafComboBox = new JComboBox(LAFNAME);
        bmkPathTextField = new JTextField();
        bmkPathButton = new JButton("Durchsuchen");
        
        // Add the 2 labels
        c.gridx = 0;
        c.fill = c.HORIZONTAL;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        c.weightx = 1.0;
        c.weighty = 0.0;
        add(compilerPanel,c);
        c.gridy = 1;
        add(guiPanel,c);
        
        
        // Layout the compilerPanel
        c.fill = c.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.0;
        compilerPanel.add(new JLabel("BMK-Pfad"),c);
        c.gridy = 1;
        c.gridy = 0;
        c.gridx = 1;
        c.weightx = 1.0;
        compilerPanel.add(bmkPathTextField,c);
        c.gridx = 2;
        c.weightx = 0.0;
        compilerPanel.add(bmkPathButton,c);
        c.gridy = 1;
        c.gridx = 1;
        c.weightx = 1.0;
        
        
        // Layout the guiPanel
        c.weightx = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        c.fill = c.BOTH;
        guiPanel.add(lafComboBox,c);

        c.gridy = 1;
        guiPanel.add(new JLabel("(Erfordert Neustart)"),c);
        
        bmkPathButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                
                JFileChooser chooser = new JFileChooser();
                
                int returnVal = chooser.showOpenDialog(MiscTab.this);
                
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    
                    bmkPathTextField.setText(chooser.getSelectedFile().getAbsolutePath());
                 
                }
                
            }
        });
        
    }
    
    
    public void load() {
        
        String laf = Config.getProperty("ui.lookandfeel");
        for (int i=0;i<LAF.length;i++)
            if (LAF[i].equals(laf))
                lafComboBox.setSelectedIndex(i);

        bmkPathTextField.setText(Config.getProperty("system.bmk"));
    }
    public void save() {
        Config.setProperty("ui.lookandfeel",LAF[lafComboBox.getSelectedIndex()]);
        Config.setProperty("system.bmk",bmkPathTextField.getText());
    }
    
}
